package com.mwj.gulimall.prodict.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.mwj.gulimall.prodict.entity.AttrEntity;
import com.mwj.gulimall.prodict.service.AttrAttrgroupRelationService;
import com.mwj.gulimall.prodict.service.AttrService;
import com.mwj.gulimall.prodict.service.CategoryService;
import com.mwj.gulimall.prodict.vo.AttrGroupRelationVo;
import com.mwj.gulimall.prodict.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.mwj.gulimall.prodict.entity.AttrGroupEntity;
import com.mwj.gulimall.prodict.service.AttrGroupService;
import com.mwj.common.utils.PageUtils;
import com.mwj.common.utils.R;



/**
 * 属性分组
 *
 * @author mwj
 * @email mwj@gmail.com
 * @date 2022-09-12 21:06:13
 */
@RestController
@RequestMapping("/product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired

    private AttrAttrgroupRelationService relationService;

    //添加分组
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
        relationService.saveBatch(vos);
        return  R.ok();

    }


    //移除分组
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vo){
        attrService.deleteRelation(vo);
        return R.ok();
    }

    /*
     查询分组里面的关联信息
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
     List<AttrEntity> entities = attrService.getReationAttr(attrgroupId);
     return R.ok().put("data",entities);
    }
    /*
      查询没有分组得消息
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params){
        PageUtils page  = attrService.getNoRelation(params,attrgroupId);
        return R.ok().put("page",page);
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
//   @RequiresPermissions("gulimall:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
//   @RequiresPermissions("gulimall:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] path= categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//   @RequiresPermissions("gulimall:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//   @RequiresPermissions("gulimall:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//  @RequiresPermissions("gulimall:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    @GetMapping("/{catlogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catlogId") Long catlogId){
         //1.查询所有分类下的分组信息
        //2.查询每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> vos =  attrGroupService.getAttrGroupWithAttrsByCatlogId(catlogId);
        return R.ok().put("data",vos);
     }

}
